package com.atguigu.mall.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.mall.product.dao.BrandDao;
import com.atguigu.mall.product.entity.BrandEntity;
import com.atguigu.mall.product.service.BrandService;
import com.atguigu.mall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;


@Service("brandService")
@Transactional(rollbackFor = Exception.class)
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String) params.get("name");
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>().like(StrUtil.isNotBlank(name),"name",name)
        );
        return new PageUtils(page);
    }

    @Override
    public void updateBrand(BrandEntity brand) {
            this.updateById(brand);
        Long brandId = brand.getBrandId();
        String brandName = brand.getName();
        this.categoryBrandRelationService.updateBrandNameByBrandId(brandId,brandName);
    }

}